package com.hbx.utils;

import com.hbx.config.properties.MailProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/*
 * @Author:hbx
 * 
 * */
@Component
public class MailSendUtil {

    @Autowired
    private MailProperties mailProperties;

    @Resource
    private JavaMailSender javaMailSender;

    /**
     * 邮件发送
     * @param email
     * @param content
     */
    @Async
    public void send(String subject, String[] email,String content) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 发送人
        helper.setFrom("资源共享平台运营中心<" + mailProperties.getUsername() + ">");
        // 设置主题
        helper.setSubject(subject);
        // 设置发送内容
        helper.setText(content, true);
        // 设置接收人
        helper.setTo(email);
        // 设置发送时间
        helper.setSentDate(new Date());
        // 发送
        javaMailSender.send(mimeMessage);
    }
}
